﻿// Developer Express Code Central Example:
// World Wide CRM Demo
// 
// The World Wide CRM demo is a next generation application with outstanding
// navigation capabilities and shows a reimagined view of customer contact and
// management. This demo showcases many of the most popular features of the
// WinForms Subscription.
// 
// You can find sample updates and versions for different programming languages here:
// http://www.devexpress.com/example=E3993

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.CrmDemo.Win.Data;
using DevExpress.CrmDemo.Win.Helpers;

namespace DevExpress.CrmDemo.Win.Controls
{
    public partial class Start : BasePage
    {
        public Start()
        {
            InitializeComponent();
        }

        private void tileNewOpp_ItemClick(object sender, DevExpress.XtraEditors.TileItemEventArgs e)
        {
            OnNavRequest(new NavRequestEventArgs(States.Dashboard));
        }

        private void Start_Load(object sender, EventArgs e)
        {
            generalStatsBindingSource.DataSource = DataProvider.Instance.GeneralStats;
            newOppsListBindingSource.DataSource = DataProvider.Instance.NewestOpportunities;
            teamMembersBindingSource.DataSource = DataProvider.Instance.TeamMembers;
        }

        private void newOppsListBindingSource_ListChanged(object sender, ListChangedEventArgs e)
        {
            RefreshNewOppTile((BindingList<Opportunity>)newOppsListBindingSource.DataSource);
        }

        private void generalStatsBindingSource_CurrentItemChanged(object sender, EventArgs e)
        {
            RefreshData((GeneralStats)generalStatsBindingSource.DataSource);
        }

        private void RefreshNewOppTile(BindingList<Opportunity> newOpps)
        {
            for (int i = 0; i < newOpps.Count(); i++)
            {
                Opportunity opp  = newOpps[i];
                tileItemNewOpps.Frames[i].Text3 = string.Format("<size=10>{0}\n<b>{1}</b>\n{2:C0}                          {3}</size>", 
                    opp.AccountName, opp.Name, opp.CostSize, opp.Technology);
            }
        }

        private void RefreshData(GeneralStats stats)
        {
            if (stats == null)
                return;
            
            tileItem_Ledger.Text2 = "<size=19>FY " + DateTime.Now.ToFiscalYear() + "</size>";

            tileItemAvgContractSize.Text3 = "<size=18>" + stats.AvgContractSize.ToString("C0") + "</size>";

            tileItemAvgTeamSize.Text3 = "<size=18>" + stats.AvgTeamSize.ToString("F0") + " people</size>";

            tileItemAvgDuration.Text3 = "<size=18>" + stats.AvgWeeks.ToString("F0") + " weeks</size>";

            tileItemYtdDealsClosed.Text3 = "<size=38>" + stats.YtdClosedCount.ToString() + "</size>";

            tileItemTeamMember1.Text2 = "<size=38>" + stats.TeamMemberOpenOppsCount0 + "</size>";
            tileItemTeamMember2.Text2 = "<size=38>" + stats.TeamMemberOpenOppsCount1 + "</size>";
            tileItemTeamMember3.Text2 = "<size=38>" + stats.TeamMemberOpenOppsCount2 + "</size>";
            tileItemTeamMember4.Text2 = "<size=38>" + stats.TeamMemberOpenOppsCount3 + "</size>";

            Color yellow = Color.FromArgb(209, 159, 4);
            Color green = Color.FromArgb(65, 126, 17);

            int[] oppsCounts = new int[]
            {
                stats.TeamMemberOpenOppsCount0,
                stats.TeamMemberOpenOppsCount1,
                stats.TeamMemberOpenOppsCount2,
                stats.TeamMemberOpenOppsCount3
            };

            int minOpenOpps = oppsCounts.Min();
            int maxOpenOpps = oppsCounts.Max();

            // If everyone has the same amount, then nobody is yellow
            if (minOpenOpps == maxOpenOpps)
                minOpenOpps = -1;

            tileItemTeamMember1.Appearance.BackColor = tileItemTeamMember1.Appearance.BorderColor = stats.TeamMemberOpenOppsCount0 == minOpenOpps ? yellow : green;
            tileItemTeamMember2.Appearance.BackColor = tileItemTeamMember2.Appearance.BorderColor = stats.TeamMemberOpenOppsCount1 == minOpenOpps ? yellow : green;
            tileItemTeamMember3.Appearance.BackColor = tileItemTeamMember3.Appearance.BorderColor = stats.TeamMemberOpenOppsCount2 == minOpenOpps ? yellow : green;
            tileItemTeamMember4.Appearance.BackColor = tileItemTeamMember4.Appearance.BorderColor = stats.TeamMemberOpenOppsCount3 == minOpenOpps ? yellow : green;                
        }

        void RefreshTeamMemberBioData()
        {
            // This assumes there are 4 team members - this should be updated if team members is ever editable

            BindingList<TeamMember> teamMembers = DataProvider.Instance.TeamMembers;

            tileItemTeamMember1.Text = teamMembers[0].FullName;
            tileItemTeamMember2.Text = teamMembers[1].FullName;
            tileItemTeamMember3.Text = teamMembers[2].FullName;
            tileItemTeamMember4.Text = teamMembers[3].FullName;

            tileItemTeamMember1.Image = teamMembers[0].PhotoSmall;
            tileItemTeamMember2.Image = teamMembers[1].PhotoSmall;
            tileItemTeamMember3.Image = teamMembers[2].PhotoSmall;
            tileItemTeamMember4.Image = teamMembers[3].PhotoSmall;
        }

        private void teamMembersBindingSource_CurrentItemChanged(object sender, EventArgs e)
        {
            RefreshTeamMemberBioData();
        }
    }
}